// LibraryServiceImpl.java
package Object.work.service;

import Object.work.exception.BookNotFoundException;
import Object.work.exception.DuplicateBookException;
import Object.work.exception.InvalidOperationException;
import Object.work.exception.UserNotFoundException;
import Object.work.model.Library;
import Object.work.model.LibraryItem;
import Object.work.model.User;

import java.util.List;

public class LibraryServiceImpl implements LibraryService {
    private Library library;

    public LibraryServiceImpl(Library library) {
        this.library = library;
    }

    // 图书CRUD操作
    @Override
    public void addBook(LibraryItem item) throws DuplicateBookException {
        library.addItem(item);
    }

    @Override
    public LibraryItem getBookById(String id) throws BookNotFoundException {
        return library.findItemById(id);
    }

    @Override
    public void updateBook(String id, LibraryItem newItem) throws BookNotFoundException, DuplicateBookException {
        library.updateItem(id, newItem);
    }

    @Override
    public void deleteBook(String id) throws BookNotFoundException {
        library.deleteItem(id);
    }

    @Override
    public List<LibraryItem> getAllBooks() {
        return library.getAllItems();
    }

    @Override
    public List<LibraryItem> searchBooksByTitle(String keyword) {
        return library.searchItemsByTitle(keyword);
    }

    // 用户CRUD操作
    @Override
    public void addUser(User user) throws DuplicateBookException {
        library.addUser(user);
    }

    @Override
    public User getUserById(String id) throws UserNotFoundException {
        return library.findUserById(id);
    }

    @Override
    public void updateUser(String id, User newUser) throws UserNotFoundException, DuplicateBookException {
        library.updateUser(id, newUser);
    }

    @Override
    public void deleteUser(String id) throws UserNotFoundException {
        library.deleteUser(id);
    }

    @Override
    public List<User> getAllUsers() {
        return library.getAllUsers();
    }

    @Override
    public List<User> searchUsersByName(String keyword) {
        return library.searchUsersByName(keyword);
    }

    // 借还书操作
    @Override
    public void borrowBook(String bookId, String userId)
            throws BookNotFoundException, UserNotFoundException, InvalidOperationException {
        library.borrowItem(bookId, userId);
    }

    @Override
    public void returnBook(String bookId) throws BookNotFoundException, InvalidOperationException {
        library.returnItem(bookId);
    }

    // 显示方法
    @Override
    public void displayAllBooks() {
        displayBooks(getAllBooks());
    }

    @Override
    public void displayAllUsers() {
        displayUsers(getAllUsers());
    }

    @Override
    public void displayBooks(List<LibraryItem> books) {
        if (books.isEmpty()) {
            System.out.println("No books found.");
        } else {
            System.out.println("Books (" + books.size() + "):");
            books.forEach(System.out::println);
        }
    }

    @Override
    public void displayUsers(List<User> users) {
        if (users.isEmpty()) {
            System.out.println("No users found.");
        } else {
            System.out.println("Users (" + users.size() + "):");
            users.forEach(System.out::println);
        }
    }
}